package com.xqx.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author：xingquanxiang createTime：2019/10/21 16:27
 * description:
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //判断session中是否存在用户名和密码
        if (session.getAttribute("username")!=null || session.getAttribute("password")!=null){
            //都存在
            return true;
        }else{
            session.setAttribute("targetUri",request.getRequestURI());
            //任意一个不存在
            response.sendRedirect(request.getContextPath()+"/login.html");
            return false;
        }
    }

}
